package com.example.content.fragment.main

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.MSG_CHART_TYPE
import com.example.content.MSG_OFFICIAL_ACTIVITY_TYPE
import com.example.content.MSG_REWARD_TYPE
import com.example.content.R
import com.example.content.activity.ChatActivity
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.SimpleListAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.databinding.DropDownMenuBinding
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.manager.ActivityMgr
import com.example.content.viewmodule.main.CommonHomeViewModel
import com.hyphenate.easeui.constants.EaseConstant
import java.util.*

class MessageFragment : BaseMvvmFragment<DropDownMenuBinding, CommonHomeViewModel>() {

    private val headers = arrayOf("全部消息")
    private val popViews = ArrayList<ListView>()
    private val listTitles = arrayOf("全部消息", "通知", "私信", "打赏")

    override fun initView() {
//        ImmersionBar.with(this).statusBarView(dataBinding.view).statusBarDarkFont(true, 0.2f).init()
        val msgListView = context?.let { ListView(it) }
        val adapter = context?.let { SimpleListAdapter(it) }
        adapter?.data?.clear()
        adapter?.data?.addAll(listTitles)
        msgListView?.dividerHeight = 0
        msgListView?.adapter = adapter

        msgListView?.setOnItemClickListener { _, _, position, _ ->
            adapter?.selectPosition = position
            dataBinding.dropMenu.setTabMenuText(listTitles[position])
            dataBinding.dropMenu.closeMenu()
        }
        msgListView?.let { popViews.add(it) }

        val msgAdapter = RecyclerAdapter(object : BindListener<String> {
            override fun convert(holder: BaseViewHolder, item: ItemEntity<String>) {

            }
        })
        msgAdapter.addItemViewByType(
            MSG_OFFICIAL_ACTIVITY_TYPE,
            R.layout.item_official_activity_layout
        )
        msgAdapter.addItemViewByType(MSG_CHART_TYPE, R.layout.item_msg_layout)
        msgAdapter.addItemViewByType(MSG_REWARD_TYPE, R.layout.item_msg_reward_layout)
//        msgAdapter.data.add(ItemEntity(12, ""))
//        msgAdapter.data.add(ItemEntity(12, ""))
        msgAdapter.data.add(ItemEntity(13, ""))
        msgAdapter.data.add(ItemEntity(13, ""))
        msgAdapter.data.add(ItemEntity(13, ""))
//        msgAdapter.data.add(ItemEntity(14, ""))
//        msgAdapter.data.add(ItemEntity(14, ""))
//        msgAdapter.data.add(ItemEntity(13, ""))
//        msgAdapter.data.add(ItemEntity(13, ""))
//        msgAdapter.data.add(ItemEntity(13, ""))

        msgAdapter.setOnItemClickListener { innerAdapter, view, position ->
            val itemType = innerAdapter.getItemViewType(position)
            if (itemType == MSG_CHART_TYPE) {
                val bundle = Bundle()
                bundle.putString(EaseConstant.EXTRA_USER_ID, "deryou")
                bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE)
                ActivityMgr.startActivity(ChatActivity::class.java, bundle)
            }
        }

        val containerView = LayoutInflater.from(context).inflate(R.layout.recyclerview_layout, null)
        val recyclerView = containerView.findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = msgAdapter
        dataBinding.dropMenu.setDropDownMenu(headers, popViews as List<View>, containerView)
        val clazz = dataBinding.dropMenu::class.java
        val mTabMenuView = clazz.getDeclaredField("mTabMenuView")
        mTabMenuView.isAccessible = true
        val layout = mTabMenuView.get(dataBinding.dropMenu) as LinearLayout
        layout.gravity = Gravity.CENTER
        if (layout.childCount > 0) {
            val view = layout.getChildAt(0)
            val params = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            view.layoutParams = params
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() = MessageFragment()
    }

    override fun getLayoutId() = R.layout.drop_down_menu
    override fun getViewModuleClass(): Class<CommonHomeViewModel> {
        return CommonHomeViewModel::class.java
    }
}